import { GlobalClass } from "../../../../../com/mui/core/GlobalClass";
import { ColorConfig } from "../../../resource/ColorConfig";
import { FilterConst } from "../../../../component/gconst/FilterConst";
import { GBox } from "../../../../../com/mui/containers/GBox";
import { GBoxDirection } from "../../../../../com/mui/containers/globalVariable/GBoxDirection";
import { ObjectPool } from "../../../../../com/gengine/utils/pools/ObjectPool";
import { TaskTreeNode } from "./TaskTreeNode";
import { InvalidationType } from "../../../../../fl/core/InvalidationType";
import { TaskInfo } from "../../../model/TaskInfo";
type int = number;
//class TaskTreeBranch
    
    export  class TaskTreeBranch /* flash.display.Sprite */
    {
        constructor()
        {
            
            buttonMode = true;
            this._openIcon = GlobalClass.getBitmapData("TreeOpenIcon");
            this._closeIcon = GlobalClass.getBitmapData("TreeCloseIcon");
            this._treeIcon = new Object /* flash.display.Bitmap */(this._openIcon);
            addChild(this._treeIcon);
            this._label = new Object /* flash.text.TextField */();
            this._label.height = 20;
            this._label.width = 140;
            this._label.autoSize = flash.text.TextFieldAutoSize.LEFT;
            this._label.x = 20;
            this._label.y = 1;
            this._label.textColor = ColorConfig.instance.getTaskColor("list_group").intColor;
            this._label.filters = [FilterConst.nameGlowFilter];
            addChild(this._label);
            this._label.mouseEnabled = false;
            this._bodyList = new GBox();
            this._bodyList.direction = GBoxDirection.VERTICAL;
            this._bodyList.x = 15;
            this._bodyList.y = 20;
            this.openTree();
            return;
        }

        public get type(): int
        {
            return this._type;
        }

        public treeChange(): void
        {
            if (this._bodyList.parent) 
            {
                this.closeTree();
            }
            else 
            {
                this.openTree();
            }
            return;
        }

        /* internal  */closeTree(): void
        {
            removeChild(this._bodyList);
            this._treeState = false;
            this._treeIcon.bitmapData = this._openIcon;
            return;
        }

        /* internal  */openTree(): void
        {
            addChild(this._bodyList);
            this._bodyList.x = 15;
            this._bodyList.y = 20;
            this._treeState = true;
            this._treeIcon.bitmapData = this._closeIcon;
            return;
        }

        public initData(arg1: Object, arg2: Array<any>): void
        {
            var loc3=null;
            var loc4=null;
            this._label.htmlText = arg1.label;
            this._type = arg1.type;
            var loc1=0;
            var loc2=arg2.length;
            while (loc1 < loc2) 
            {
                loc4 = arg2[loc1];
                (loc3 = ObjectPool.getObject(TaskTreeNode)).initTask(loc4);
                this._bodyList.addChild(loc3);
                ++loc1;
            }
            this._bodyList.layChildChanged = true;
            this._bodyList.invalidate(InvalidationType.ALL);
            if (!contains(this._bodyList)) 
            {
                this.openTree();
            }
            return;
        }

        public insertTask(arg1: TaskInfo, arg2: Object): void
        {
            if (arg2 != null) 
            {
                this._type = arg2.type;
                this._label.htmlText = arg2.label;
                if (!contains(this._bodyList)) 
                {
                    this.openTree();
                }
            }
            var loc1=ObjectPool.getObject(TaskTreeNode);
            loc1.initTask(arg1);
            this._bodyList.addChild(loc1);
            this._bodyList.layChildChanged = true;
            this._bodyList.invalidate(InvalidationType.ALL);
            return;
        }

        public delTask(arg1: int): int
        {
            var loc3=null;
            var loc1=0;
            var loc2=this._bodyList.numChildren;
            while (loc1 < loc2) 
            {
                if ((loc3 = this._bodyList.getChildAt(loc1) as TaskTreeNode).task.sTask.code == arg1) 
                {
                    loc3.dispose();
                    ObjectPool.disposeObject(loc3, TaskTreeNode);
                    this._bodyList.removeChild(loc3);
                    this._bodyList.invalidate(InvalidationType.ALL);
                    if (this._bodyList.numChildren == 0 && this.contains(this._bodyList)) 
                    {
                        removeChild(this._bodyList);
                    }
                    return arg1;
                }
                ++loc1;
            }
            return -1;
        }

        public updateTask(arg1: TaskInfo): int
        {
            var loc3=null;
            var loc1=0;
            var loc2=this._bodyList.numChildren;
            while (loc1 < loc2) 
            {
                if ((loc3 = this._bodyList.getChildAt(loc1) as TaskTreeNode).task.sTask.code == arg1.sTask.code) 
                {
                    loc3.updateTask(arg1);
                    return arg1.sTask.code;
                }
                ++loc1;
            }
            return -1;
        }

        public dispose(): void
        {
            var loc1=null;
            while (this.bodyLength > 0) 
            {
                loc1 = this._bodyList.removeChildAt(0) as TaskTreeNode;
                loc1.dispose();
                ObjectPool.disposeObject(loc1, TaskTreeNode);
            }
            return;
        }

        public get bodyLength(): int
        {
            return this._bodyList.numChildren;
        }

        public /* override */ get height(): number
        {
            var loc1=0;
            if (this._label.parent) 
            {
                loc1 = loc1 + this._label.height;
            }
            if (this._bodyList.parent) 
            {
                loc1 = loc1 + this.bodyLength * 20;
            }
            return loc1;
        }

        /* internal */ /* var */_label: Object /* flash.text.TextField */;

        /* internal */ /* var */_openIcon: Object /* flash.display.BitmapData */;

        /* internal */ /* var */_closeIcon: Object /* flash.display.BitmapData */;

        /* internal */ /* var */_treeIcon: Object /* flash.display.Bitmap */;

        /* internal */ /* var */_bodyList: GBox;

        /* internal */ /* var */_type: int;

        /* internal */ /* var */_treeState: Boolean=true;
    }
